class Solution {
public:
    int largestSumAfterKNegations(vector<int>& nums, int k) {
        sort(nums.begin(), nums.end(), [](int x, int y) { return abs(x) > abs(y); });
        for (int i = 0; i < nums.size(); ++i)
        {
            if (nums[i] < 0 && k > 0)
            {
                nums[i] *= -1;
                --k;
            }
        }
        if (k % 2 == 1) nums[nums.size() - 1] *= -1;
        int sum = 0;
        for (auto e : nums) sum += e;
        return sum;
    }
};